import { createVNode } from "vue";
import { ExclamationCircleOutlined } from "@ant-design/icons-vue";
import { Modal, message } from "ant-design-vue";
import {
  getListApi,
  disabledPickupAddressApi,
  enabledPickupAddressApi,
  getNoFbaAddressListApi,
  disabledNoFbaAddressApi,
  enabledNoFbaAddressApi,
  deleteNoFbaAddressApi,
  deletePickupAddressApi
} from "@/api/client/configCenter/addressManagement";
export default function OperationHook(state: Base.GeneraDataType) {
  const { searchForm, tableData, dialogOpts, loading, activeKey } = state;
  const getData = () => {
    const params = {
      ...searchForm
    };
    loading.value = true;
    let requestFn;
    if (activeKey.value === "1") requestFn = getListApi;
    else requestFn = getNoFbaAddressListApi;
    tableData.value = [];
    return requestFn(params)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          tableData.value = data.records || [];
          searchForm.total = Number(data.total);
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  const searchHandle = () => {
    searchForm.pageNum = 1;
    getData();
  };
  const changeTabHandle = () => {
    searchHandle();
  };
  const editHandle = (row: CountryRegion.ListRows) => {
    dialogOpts.title = "编辑";
    dialogOpts.width = "520px";
    dialogOpts.type = 2;
    dialogOpts.component = activeKey.value === "1" ? "AddPickupAddress" : "AddNoFbaAddress";
    dialogOpts.passData = row;
    dialogOpts.visible = true;
  };
  const addHandle = () => {
    dialogOpts.title = "新增";
    dialogOpts.width = "520px";
    dialogOpts.type = 1;
    dialogOpts.component = activeKey.value === "1" ? "AddPickupAddress" : "AddNoFbaAddress";
    dialogOpts.visible = true;
  };
  // 修改状态
  const changeStatusHandle = (row: CountryRegion.ListRows) => {
    const params: any = {
      id: row.id
    };
    let tip = null;
    let requestFn: any;
    if (activeKey.value === "1") {
      if (row.status === 1) {
        tip = "是否停用该取件地址?";
        params.status = 0;
        requestFn = disabledPickupAddressApi;
      } else {
        tip = "是否启用该取件地址?";
        params.status = 1;
        requestFn = enabledPickupAddressApi;
      }
    } else {
      if (row.status === 1) {
        tip = "是否停用该非FBA收件地址?";
        params.status = 0;
        requestFn = disabledNoFbaAddressApi;
      } else {
        tip = "是否启用该非FBA收件地址?";
        params.status = 1;
        requestFn = enabledNoFbaAddressApi;
      }
    }
    Modal.confirm({
      title: tip,
      icon: createVNode(ExclamationCircleOutlined),
      content: "",
      okText: "确定",
      okType: "danger",
      cancelText: "取消",
      onOk() {
        requestFn(params).then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("操作成功!");
            getData();
          }
        });
      }
    });
  };
  const deleteHandle = (row: ClientAddressManagement.ListRows) => {
    const params: any = {
      id: row.id
    };
    let tip = null;
    let requestFn: any;
    if (activeKey.value === "1") {
      tip = "是否删除该取件地址?";
      requestFn = deletePickupAddressApi;
    } else {
      tip = "是否删除该非FBA收件地址?";
      requestFn = deleteNoFbaAddressApi;
    }
    Modal.confirm({
      title: tip,
      icon: createVNode(ExclamationCircleOutlined),
      content: "",
      okText: "确定",
      okType: "danger",
      cancelText: "取消",
      onOk() {
        requestFn(params).then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("操作成功!");
            getData();
          }
        });
      }
    });
  };
  return {
    searchHandle,
    getData,
    editHandle,
    addHandle,
    changeStatusHandle,
    changeTabHandle,
    deleteHandle
  };
}
